package cn.swenty.backstage.base.web.system.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import cn.swenty.backstage.base.web.system.entity.SysResFormMap;
import cn.swenty.backstage.base.web.system.entity.SysRoleResFormMap;
import cn.swenty.backstage.base.web.system.mapper.FunctionMapper;
import cn.swenty.backstage.base.web.system.mapper.SysRoleFunctionMapper;
import cn.swenty.backstage.framework.common.model.Function;
import cn.swenty.backstage.framework.common.util.Bean2MapUtils;
import cn.swenty.backstage.framework.common.util.StringUtil;
import cn.swenty.backstage.framework.common.util.TreeUtil;
import cn.swenty.backstage.framework.database.entity.Entity;
import cn.swenty.backstage.framework.service.BaseService;
import cn.swenty.backstage.framework.util.IdGenerator;
@Service
public class FunctionService extends BaseService {
	@Inject
	private FunctionMapper  functionMapper;
	
	@Inject
	private SysRoleFunctionMapper roleFunctionMapper;
	
	public List<Function> getUserTreeFunction(String userid){
		
		SysResFormMap formMap = new SysResFormMap();
		formMap.put("user_id", userid);
		
		List<SysResFormMap> list = functionMapper.findByUserId(formMap);
		
		List<Function> functions = null;
		try {
			functions = Bean2MapUtils.maps2Objects(list, Function.class);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("SysUserFormMap can't convert to user "+e.getMessage());
		}
		return TreeUtil.createTree(functions);
	}
	
	
	
	public void editEntity(SysResFormMap formMap){
		try {
			formMap.update();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("SysResFormMap update to error"+e.getMessage());
		}
	}
	
	/**
	 * 保存角色功能权限
	 * @param role_ids
	 * @param function_id
	 */
	public void saveRoleFunction(String[] role_ids,String function_id){
		
		//先删除
		dropRoleDown(function_id, role_ids);
		if(role_ids!=null && role_ids.length>0){
			//后增加
			addRoleUp(function_id, role_ids);
		}
	}
	
	/**
	 * 
	 * 递归删除不在角色里的权限
	 * @param function_id
	 * @param role_ids
	 */
	public void dropRoleDown(String function_id,String []role_ids){
	    SysResFormMap  resFormMap =new SysResFormMap();
	    resFormMap.put("parent_id", function_id);
		List<SysResFormMap> formMaps = functionMapper.findByParent(resFormMap);
		for(int i=0;i<formMaps.size();i++){
			dropRoleDown(formMaps.get(i).getStr("function_id"), role_ids);
		}
		SysRoleResFormMap roleResFormMap = new SysRoleResFormMap();
		roleResFormMap.put("function_id", function_id);
		roleResFormMap.put("role_id", role_ids);
		//删除关系表
		roleFunctionMapper.deleteByNotRoles(roleResFormMap);
		
	}
	
	public void addRoleUp(String function_id,String []role_ids){
		SysResFormMap srfm = this.get(SysResFormMap.class, function_id);
		if(srfm!=null && StringUtil.isNotNull((String)srfm.get("parent_id"))&&!srfm.getStr("parent_id").equals("0")){
			addRoleUp(srfm.getStr("parent_id"),role_ids);
		}
		for(String role_id:role_ids){
			SysRoleResFormMap formMap = new SysRoleResFormMap();
			formMap.put("role_id", role_id);
			formMap.put("function_id", function_id);
			List<SysRoleResFormMap> formMaps = roleFunctionMapper.findByRoleFunction(formMap);
			if(formMaps==null||formMaps.size()==0){
				formMap.put("id", IdGenerator.getNextId());
				this.save(formMap);
			}
		}
	}
	
	
	/**
	 * 按照id删除菜单，删除该菜单下所有子集菜单
	 * @param ids
	 */
	public void deleteByIds(String[] ids){
		for(int i=0;i<ids.length;i++){
			dropChildren(ids[i]);
		}
	}
	
	/**
	 * 递归删除子集
	 * 
	 * @param id
	 */
	public void dropChildren(String id){
		SysResFormMap resFormMap = new SysResFormMap();
		resFormMap.put("parent_id", id);
		List<SysResFormMap> resFormMaps = functionMapper.findByParent(resFormMap);
		for(int i=0;i<resFormMaps.size();i++){
			System.out.println(resFormMaps.get(i).getStr("function_id"));
			dropChildren(resFormMaps.get(i).getStr("function_id"));
			
		}
		this.deleteById(SysResFormMap.class, id);
	}
	
	
	/**
	 * 通过id查询
	 * 
	 * @param roleId
	 * @return
	 */
	public List<SysResFormMap> findByRole(String roleId){
		SysResFormMap formMap = new SysResFormMap();
		formMap.put("role_id", roleId);
		return functionMapper.findByRole(formMap);
	}
	
	 /**
	  * 
	  * @param sysResFormMap
	  * @return
	  */
	public List<SysResFormMap> findResHiddens(SysResFormMap sysResFormMap){
		return functionMapper.findResHiddens(sysResFormMap);
	}
	
	
	
	
	public List<? extends Entity> findByParent(String parentId){
		SysResFormMap formMap = new SysResFormMap();
		formMap.put("parent_id", parentId);
		return this.queryList(formMap);
		
	}
	
	public Integer queryMaxSqe(){
		return functionMapper.findMaxSquence();
	}
	
}
